package com.dudy.authdemo.service.impl;

import com.dudy.authdemo.mapper.RolePermissionMapper;
import com.dudy.authdemo.model.RolePermission;
import com.dudy.authdemo.service.RolePermissionService;
import com.dudy.authdemo.vo.RolePermissionVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class RolePermissionServiceImpl extends BaseServiceImpl<RolePermission, RolePermissionVO> implements RolePermissionService {
    @Autowired
    private RolePermissionMapper rolePermissionMapper;

    @Override
    public void insertBatch(List<RolePermissionVO> rolePermissionVOList) {
        Example example = new Example(RolePermission.class);
        example.createCriteria().andEqualTo("roleCode", rolePermissionVOList.get(0).getRoleCode());
        rolePermissionMapper.deleteByExample(example);
        rolePermissionVOList.forEach(rolePermissionVO -> {
            RolePermission rolePermission = new RolePermission();
            BeanUtils.copyProperties(rolePermissionVO, rolePermission);
            rolePermissionMapper.insert(rolePermission);
        });
    }

    @Override
    public RolePermissionVO create() {
        return new RolePermissionVO();
    }
}